package com.example.review;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.review.service.MyIntentService;
import com.example.review.service.MyService;
import com.example.review.service.ReceptionService;

public class chapterTen4Activity extends AppCompatActivity {

    private ReceptionService.DownloadBinder downloadBinder;



    /**
     * 创建了一个ServiceConnection 的匿名类
     * 在里面重写了 onServiceConnected() 方法和onServiceDisconnected() 方法
     */
    private ServiceConnection connection =new ServiceConnection() {
        @Override
//        活动与服务成功绑定调用
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //向下转型得到了DownloadBinder 的实例
            downloadBinder = (ReceptionService.DownloadBinder) iBinder;
            //得到了DownloadBinder 的实例 现在我们可以在活动中根据具体的场景来调用DownloadBinder
            //中的任何public 方法，即实现了指挥服务干什么服务就去干什么的功能
            downloadBinder.startDownload();
            downloadBinder.getProgress();
        }

        @Override
        //活动与服务的连接断开的时候调用
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_ten4);
        Button button = findViewById(R.id.start_ser2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(chapterTen4Activity.this, ReceptionService.class);
                startService(intent);
            }
        });

        Button button1 = findViewById(R.id.stop_ser2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(chapterTen4Activity.this, ReceptionService.class);
                stopService(intent);
            }
        });

        Button bindService = (Button) findViewById(R.id.bind_service2);
        bindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent bindIntent = new Intent(chapterTen4Activity.this, ReceptionService.class);
                // 绑定服务 三个参数 intent  的ServiceConnection的实例
                //第三个参数则 是一个标志位，这里传入BIND_AUTO_CREATE 表示在活动和服务进行绑定后自动创建服务
                //这会使得MyService中的onCreate() 方法得到执行，但onStartCommand() 方法不会执行
                bindService(bindIntent, connection, BIND_AUTO_CREATE);
            }
        });

        Button unbindService = (Button) findViewById(R.id.unbind_service2);
        unbindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(connection); // 解绑服务
            }
        });


        Button startIntentService = (Button) findViewById(R.id.start_intent_service);
        startIntentService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 打印主线程的id
                Log.d("chapterTen4Activity", "Thread id is " + Thread.currentThread().getId());
                Intent intentService = new Intent(chapterTen4Activity.this, MyIntentService.class);
                startService(intentService);
            }
        });
    }
}